#ifndef _FUNC_LEXER_H_
# define _FUNC_LEXER_H_

#include "func_lexer.h"

#define W3L_PARAMETERS int w3l_arity, char **w3l_args
#define W3L_FUNC_RETURN cmd_ret *
#define W3L_FUNCTION(fname) W3L_FUNC_RETURN w3l_ ## fname ( W3L_PARAMETERS )
#define W3L_COMMAND(fname, fhelp, ...) w3l_load_command(#fname,fhelp,w3l_ ## fname,__VA_ARGS__);

#define W3L_ERR_OK              "OK"
#define W3L_ERR_NULL            "nil"
#define W3L_ERR_GENERAL         "Error"
#define W3L_ERR_ILL_FORMED      "Ill Formed Function"
#define W3L_ERR_ARITY           "Wrong Arity Count"
#define W3L_ERR_FUNC_NOT_FOUND  "Function not Found"
#define W3L_ERR_USR_NOT_FOUND   "User not found"
#define W3L_ERR_DOC_NOT_FOUND   "Document not found"
#define W3L_ERR_NOT_IMPLEMENTED "This has not yet been implemented. Sorry."


#define W3L_FUNC_ERR(msg) construct_return(msg,DATA_TYPE_ERROR,strlen(msg),DO_NOT_FREE_DATA)

#define FUNCTION_TREE_HASH_SIZE 128

#define FREE_DATA 1
#define DO_NOT_FREE_DATA 0

typedef struct {
	void *data;
	char data_type;
	long data_length;
	char free_data;
} cmd_ret;

typedef struct {
	char *fname;
	char *fdesc;
	int arity;
	cmd_ret *(*handler)(W3L_PARAMETERS);
	char **arguments;
} w3l_cmd_entry;

typedef struct _w3live_func_tree_node {
	w3l_cmd_entry *cmd_entry;
	struct _w3live_func_tree_node *children[128];
} func_tree_node;

cmd_ret *construct_return(void *data,char data_type,long data_length,char free_data);

void destroy_return(cmd_ret *cmd);

func_tree_node *init_func_tree_node();

void lexer_insert ( w3l_cmd_entry *cmd );

LNode_type *list_functions();

w3l_cmd_entry *lexer_fetch ( char *fname );

void arg_parse(char *arg_string, char ***args, int *arity);

cmd_ret *parse( char *line );

void w3l_load_command(char *fname,char *fdesc, W3L_FUNC_RETURN(*handler)(W3L_PARAMETERS),int arity,...);

#endif
